package homework.module3;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManageSystem {

    static List<Student> studentList = new ArrayList<>();

    public static void printStudentInfo(){
        if (studentList.size()>0){
            for (Student student: studentList){
                String name = student.getName();
                String id = student.getStudentId();
                System.out.println("学生：" + name + "，id：" + id);
            }
        }else{
            System.out.println("没有学生数据");
        }
    }

    public static void addStudent(){
        System.out.println("请输入要增加的学生姓名和id，例如：大黄,3");
//        Scanner sc = new Scanner(System.in);
//        String input = sc.nextLine();

        while(true){
            Scanner sc = new Scanner(System.in);
            String input = sc.nextLine();
            if (input.contains(",")){
                String[] nameId = input.split(",");
                String studentName = nameId[0];
                String studentId = nameId[1];
                Student newStudent = new Student(studentName, studentId);
                studentList.add(newStudent);
                System.out.println("成功添加了一位学生！");
                break;
            }else{
                System.out.println("格式错误，请重新输入");
            }
        }

    }

    public static void deleteStudent(){

        System.out.println("请输入要删除的学生的id，当前学生情况如下");
        printStudentInfo();  // 打印所有学生情况

        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        for (int i=0;i<studentList.size();i++){
            Student student = studentList.get(i);
            String name = student.getName();
            String id = student.getStudentId();
            if (input.equals(id)){
                studentList.remove(i); // 删除学生
                System.out.println("删除学生：" + name + "，id：" + id);
            }
        }

    }

    public static void findStudent(){

        System.out.println("请输入要查找的学生的id，当前学生情况如下");
        printStudentInfo();  // 打印所有学生情况

        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        for (Student student: studentList){
            String name = student.getName();
            String id = student.getStudentId();
            if (input.equals(id)){
                System.out.println("查找到的学生：" + name + "，id：" + id);
                break;
            }
        }

    }

    public static void main(String[] args) {

        System.out.println("学生数据已初始化，请输入需要进行的指令：1、增加学生，2、删除学生，3、查找学生信息，4、显示所有学生信息，5、退出");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        System.out.println("输入的内容：" + input);


        Student s1 = new Student("小李", "1");
        Student s2 = new Student("隔壁老王", "2");

        studentList.add(s1);
        studentList.add(s2);

        while(input!=null){
            if(input.equals("1")){ // 增加学生
                addStudent();
            }else if(input.equals("2")){
                deleteStudent();
            }else if(input.equals("3")){
                findStudent();
            }else if(input.equals("4")){
                printStudentInfo();
            }else if(input.equals("5")){
                System.out.println("退出系统");
                break;
            }
            System.out.println("学生数据已初始化，请输入需要进行的指令：1、增加学生，2、删除学生，3、查找学生信息，4、显示所有学生信息，5、退出");
            input = sc.nextLine();
        }


    }
}

class Student{

    String name;
    String studentId;

    public Student(String name, String studentId){
        this.name = name;
        this.studentId = studentId;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }


}